package com.zy.gateway.predicateFactory;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @program: shopping153_cloud
 * @description:
 * @author: zy
 * @create: 2025-08-27 16:54
 */
@Component
public class TimeLimitBetweenRoutePredicateFactory extends AbstractRoutePredicateFactory<TimeLimitBetweenRoutePredicateFactory.Config> {
    public static final String DATETIME1_KEY = "datetime1";

    /**
     * DateTime 2 key.
     */
    public static final String DATETIME2_KEY = "datetime2";

    public TimeLimitBetweenRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(DATETIME1_KEY, DATETIME2_KEY);
    }


//    @Override
//    public Predicate<ServerWebExchange> apply(Config config) {
//        return new GatewayPredicate() {
//            @Override
//            public boolean test(ServerWebExchange serverWebExchange) {
//                return false;
//            }
//        };
//    }


    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        LocalTime startTime=config.datetime1;
        LocalTime endTime=config.datetime2;
        return    (serverWebExchange) -> {
            LocalTime now=LocalTime.now();
            return now.isAfter(startTime)&&now.isBefore(endTime);
        };
    }

    public static class Config{
        private LocalTime datetime1;
        private LocalTime datetime2;

        public LocalTime getDatetime1() {
            return datetime1;
        }

        public void setDatetime1(LocalTime datetime1) {
            this.datetime1 = datetime1;
        }

        public LocalTime getDatetime2() {
            return datetime2;
        }

        public void setDatetime2(LocalTime datetime2) {
            this.datetime2 = datetime2;
        }
    }

}
